package lib.self.widget.list;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver;
import androidx.recyclerview.widget.RecyclerView.ItemAnimator;
import androidx.recyclerview.widget.RecyclerView.ItemDecoration;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.List;

import lib.self.AppEx;
import lib.self.R;
import lib.self.adapter.interfaces.IAdapter;
import lib.self.adapter.interfaces.OnAdapterClickListener;
import lib.self.adapter.interfaces.OnRecyclerItemClickListener;
import lib.self.adapter.recycler.MultiRecyclerAdapterEx;
import lib.self.util.autoFit.FitViewParamsUtil;
import lib.self.util.view.LayoutUtil;
import lib.self.util.view.ViewUtil;
import lib.self.view.recycler.WrapRecyclerView;
import lib.self.widget.list.mix.IScrollWidget;

/**
 * description: RecyclerView组件
 * author: Steven Xiao
 * date: 2017/5/23
 */
public class RecyclerWidget<T> implements OnRecyclerItemClickListener, IScrollWidget<T> {
    private WrapRecyclerView mRv;
    private IAdapter<T> mAdapter;

    private RelativeLayout mHeaderView;//头
    private RelativeLayout mFooterView;//尾
    private RelativeLayout mNoMoreDataView;//没有更多数据
    private RelativeLayout mEmptyView;//空数据

    private AdapterDataObserver mDataObserver;

    private OnRecyclerWidgetListener<T> mListener;

    public RecyclerWidget(@NonNull OnRecyclerWidgetListener<T> listener) {
        if (listener == null) {
            throw new IllegalStateException("OnRecyclerWidgetListener must be NonNull");
        }
        mListener = listener;
    }

    public void findViews(ViewGroup contentView, @IdRes int viewId, View headerView, View footerView, View noMoreDataView, View emptyView) {
        mRv = contentView.findViewById(viewId);

        LayoutInflater inflater = LayoutInflater.from(AppEx.getCt());
        // 在这里添加header和footer, 以便于接着在子类里从header和footer里findView
        if (headerView != null) {
            View layoutHeader = inflater.inflate(R.layout.listview_header_footer_root, null);
            mHeaderView = layoutHeader.findViewById(R.id.listview_header_footer_root);
            mHeaderView.addView(headerView, LayoutUtil.getRelativeParams(LayoutUtil.MATCH_PARENT, LayoutUtil.WRAP_CONTENT));
            FitViewParamsUtil.autoFitAll(layoutHeader);
            mRv.addHeaderView(layoutHeader);
        }

        if (footerView != null) {
            View layoutFooter = inflater.inflate(R.layout.listview_header_footer_root, null);
            mFooterView = layoutFooter.findViewById(R.id.listview_header_footer_root);
            mFooterView.addView(footerView, LayoutUtil.getRelativeParams(LayoutUtil.MATCH_PARENT, LayoutUtil.WRAP_CONTENT));
            FitViewParamsUtil.autoFitAll(layoutFooter);
            mRv.addFooterView(layoutFooter);
        }

        if (noMoreDataView != null) {
            View layoutNoMoreData = inflater.inflate(R.layout.listview_header_footer_root, null);
            mNoMoreDataView = layoutNoMoreData.findViewById(R.id.listview_header_footer_root);
            mNoMoreDataView.addView(noMoreDataView, LayoutUtil.getRelativeParams(LayoutUtil.MATCH_PARENT, LayoutUtil.WRAP_CONTENT));
            FitViewParamsUtil.autoFitAll(layoutNoMoreData);
            mRv.addFooterView(layoutNoMoreData);
        }

        if (emptyView != null) {
            View layoutEmpty = inflater.inflate(R.layout.listview_header_footer_root, null);
            mEmptyView = layoutEmpty.findViewById(R.id.listview_header_footer_root);
            mEmptyView.addView(emptyView, LayoutUtil.getRelativeParams(LayoutUtil.MATCH_PARENT, LayoutUtil.WRAP_CONTENT));
            FitViewParamsUtil.autoFitAll(layoutEmpty);
            hideEmptyView();
            contentView.addView(layoutEmpty, 4);
        }
    }

    public void setViewsValue(LayoutManager manager, ItemDecoration decoration, ItemAnimator animator) {
        MultiRecyclerAdapterEx adapter = (MultiRecyclerAdapterEx) mAdapter;
        mRv.setLayoutManager(manager);
        mRv.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        adapter.setEnableLongClick(mListener.enableLongClick());

        if (animator != null) {
            mRv.setItemAnimator(animator);
        }

        if (decoration != null) {
            mRv.addItemDecoration(decoration);
        }

//        if (!mListener.needDelayAddEmptyView()) {
//            addEmptyViewIfNoNull();
//        }
    }

    public void addEmptyViewIfNoNull() {
    }

    public void removeEmptyViewIfNoNull() {
    }

    public void initAdapter(IAdapter adapter) {
        if (mAdapter != null) {
            return;
        }

        mAdapter = adapter;
        mDataObserver = new AdapterDataObserver() {

            @Override
            public void onChanged() {
                mListener.onDataSetChanged();
            }
        };

//        mAdapter.registerDataSetObserver(mDataObserver);
    }

    public void onDestroy() {
        if (mAdapter != null && mDataObserver != null) {
//            mAdapter.unregisterDataSetObserver(mDataObserver);
        }
    }

    @Override
    public final void onItemClick(View v, int position) {
        int index = getItemRealPosition(position);
        if (index < 0) {
            // 点击的是header区域
            mListener.onHeaderClick(v);
            return;
        }
        if (index >= getCount()) {
            // 点击的是footer区域
            mListener.onFooterClick(v);
            return;
        }
        mListener.onItemClick(v, index);
    }

    @Override
    public void onItemLongClick(View v, int position) {
        int index = getItemRealPosition(position);
        if (index < 0) {
            // 点击的是header区域
            return;
        }
        if (index >= getCount()) {
            // 点击的是footer区域
            return;
        }
        mListener.onItemLongClick(v, index);
    }

    public void setData(List<T> data) {
        getAdapter().setData(data);
    }

    public void addItem(T item) {
        getAdapter().add(item);
    }

    public void addItem(int position, T item) {
        getAdapter().add(position, item);
    }

    public void addAll(List<T> data) {
        getAdapter().addAll(data);
    }

    public void addAll(int position, List<T> item) {
        getAdapter().addAll(position, item);
    }

    public void invalidate() {
        getAdapter().notifyDataSetChanged();
    }

    public void notifyItemChanged(int position) {
        if (mHeaderView != null) {
            position++;
        }
        getAdapter().notifyItemChanged(position);
    }

    public void remove(int position) {
        getAdapter().remove(position);
    }

    public void remove(T item) {
        getAdapter().remove(item);
    }

    public void removeAll() {
        getAdapter().removeAll();
    }

    public List<T> getData() {
        return getAdapter().getData();
    }

    public int getCount() {
        return getAdapter().getCount();
    }

    public int getLastItemPosition() {
        return getAdapter().getLastItemPosition();
    }

    public T getItem(int position) {
        return getAdapter().getItem(position);
    }

    public boolean isEmpty() {
        return getAdapter().isEmpty();
    }

    public void setOnAdapterClickListener(OnAdapterClickListener listener) {
        getAdapter().setOnAdapterClickListener(listener);
    }

    public void addOnScrollListener(OnScrollListener listener) {
        mRv.addOnScrollListener(listener);
    }

    public int getItemRealPosition(int position) {
        return position - mRv.getHeadersCount();
    }

    public int getFirstVisiblePosition() {
        return mRv.getFirstVisiblePosition();
    }

    public View getChildAt(int index) {
        return mRv.getChildAt(index);
    }

    public int getHeaderViewPosition() {
        return mRv.getHeadersCount();
    }

    @Override
    public void addFooterView(View v) {
        if (mRv != null) {
            mRv.addFooterView(v);
        }
    }

    public void showFooterView() {
        if (mFooterView != null) {
            LayoutUtil.setHeaderOrFooterParams(mFooterView, LayoutUtil.MATCH_PARENT, LayoutUtil.WRAP_CONTENT);
            mFooterView.setAlpha(1);
        }
    }

    public void hideFooterView() {
        if (mFooterView != null) {
            LayoutUtil.setHeaderOrFooterParams(mFooterView, LayoutUtil.MATCH_PARENT, LayoutUtil.MIN_POSITIVE_VALUE);
            mFooterView.setAlpha(0);
        }
    }

    public void showHeaderView() {
        if (mHeaderView != null) {
            LayoutUtil.setHeaderOrFooterParams(mHeaderView, LayoutUtil.MATCH_PARENT, LayoutUtil.WRAP_CONTENT);
            mHeaderView.setAlpha(1);
        }
    }

    public void hideHeaderView() {
        if (mHeaderView != null) {
            // 必须至少设置为1PX 否则无法刷新  无须刷新可以使用0
            LayoutUtil.setHeaderOrFooterParams(mHeaderView, LayoutUtil.MATCH_PARENT, LayoutUtil.MIN_POSITIVE_VALUE);
            mHeaderView.setAlpha(0);
        }
    }

    @Override
    public void showNoMoreDataView() {
        if (mNoMoreDataView != null) {
            LayoutUtil.setHeaderOrFooterParams(mNoMoreDataView, LayoutUtil.MATCH_PARENT, LayoutUtil.WRAP_CONTENT);
            mNoMoreDataView.setAlpha(1);
        }
    }

    @Override
    public void hideNoMoreDataView() {
        if (mNoMoreDataView != null) {
            LayoutUtil.setHeaderOrFooterParams(mNoMoreDataView, LayoutUtil.MATCH_PARENT, LayoutUtil.MIN_POSITIVE_VALUE);
            mNoMoreDataView.setAlpha(0);
        }
    }

    public void showEmptyView() {
        if (mEmptyView != null) {
            ViewUtil.showView(mEmptyView);
        }
    }

    public void hideEmptyView() {
        if (mEmptyView != null) {
            ViewUtil.goneView(mEmptyView);
        }
    }

    public void setSelection(int position) {
        mRv.scrollToPosition(position);
    }

    public void smoothScrollToPosition(int position) {
        mRv.smoothScrollToPosition(position);
    }

    public WrapRecyclerView getRv() {
        return mRv;
    }

    public IAdapter<T> getAdapter() {
        if (mAdapter == null) {
            initAdapter(mListener.initAdapter());
        }
        return mAdapter;
    }

    public boolean isAdapterNull() {
        return mAdapter == null;
    }
}
